package com.zougen202301180355.restaurant.controller;

import com.zougen202301180355.restaurant.pojo.Customer;
import com.zougen202301180355.restaurant.service.CustomersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 邹根
 * 顾客管理控制器
 */
@Controller
@RequestMapping("/customers")
public class CustomersController {

    @Autowired
    private CustomersService customersService;

    /**
     * 显示顾客列表
     */
    @GetMapping("/list")
    public String list(Model model) {
        List<Customer> customers = customersService.getAllCustomers();
        model.addAttribute("customers", customers);
        return "customers/list";
    }

    /**
     * 显示添加顾客页面
     */
    @GetMapping("/add")
    public String showAddForm(Model model) {
        model.addAttribute("customer", new Customer());
        return "customers/add";
    }

    /**
     * 处理添加顾客请求
     */
    @PostMapping("/add")
    public String addCustomer(@ModelAttribute Customer customer) {
        customersService.addCustomer(customer);
        return "redirect:/customers/list";
    }

    /**
     * 显示编辑顾客页面
     */
    @GetMapping("/edit/{id}")
    public String showEditForm(@PathVariable("id") Integer id, Model model) {
        System.out.println("===== 编辑顾客页面 =====");
        System.out.println("顾客ID: " + id);
        Customer customer = customersService.getCustomerById(id);
        System.out.println("查询到的顾客: " + customer);
        model.addAttribute("customer", customer);
        return "customers/edit";
    }

    /**
     * 处理编辑顾客请求
     */
    @PostMapping("/edit")
    public String editCustomer(@ModelAttribute Customer customer) {
        customersService.updateCustomer(customer);
        return "redirect:/customers/list";
    }

    /**
     * 删除顾客
     */
    @GetMapping("/delete/{id}")
    public String deleteCustomer(@PathVariable("id") Integer id, Model model) {
        System.out.println("===== 删除顾客 =====");
        System.out.println("顾客ID: " + id);
        try {
            customersService.deleteCustomer(id);
            return "redirect:/customers/list";
        } catch (Exception e) {
            // 外键约束异常，该顾客有订单
            if (e.getMessage().contains("foreign key constraint") || 
                e.getCause() != null && e.getCause().getMessage().contains("foreign key constraint")) {
                model.addAttribute("error", "无法删除：该顾客已有订单，请先删除相关订单。");
            } else {
                model.addAttribute("error", "删除失败：" + e.getMessage());
            }
            // 重新加载顾客列表
            model.addAttribute("customers", customersService.getAllCustomers());
            return "customers/list";
        }
    }

    /**
     * 查看顾客详情及其订单
     */
    @GetMapping("/detail/{id}")
    public String viewDetail(@PathVariable("id") Integer id, Model model) {
        System.out.println("===== 查看顾客详情 =====");
        System.out.println("顾客ID: " + id);
        Customer customer = customersService.getCustomerWithOrders(id);
        System.out.println("查询到的顾客（含订单）: " + customer);
        model.addAttribute("customer", customer);
        return "customers/detail";
    }
}
